<?php


namespace app\service;


use app\entity\GoodsEntity;
use app\models\request\ReqAddGoods;
use app\models\request\ReqGoodsPage;
use app\models\response\RespGoodsDetail;
use app\models\response\RespPage;
use yii\data\Pagination;


class GoodsService
{

    /**
     * @param $IdArr
     * @return array|RespGoodsDetail[]
     */
    public function findByIdArr($IdArr) {
        $goodsList = GoodsEntity::find()->where(["del_flag"=>0,"id"=>$IdArr])->all();
        $finalList = [];
        foreach ($goodsList as $entity) {
            $resp = new RespGoodsDetail();
            $resp->setGoodsName($entity->getGoodsName());
            $resp->setId($entity->getId());
            $resp->setDescTxt($entity->getDescTxt());
            $resp->setNeedPoints($entity->getNeedPoints());
            $resp->setPic($entity->getPic());
            $resp->setBarcode($entity->getBarcode());
            $resp->setExchangeNum($entity->getExchangeNum());
            $resp->setStockNum($entity->getStockNum());
            array_push($finalList,$resp);
        }
        return $finalList;
    }

    /**
     * @param ReqGoodsPage $req
     * @return RespPage
     */
    public function listPage(ReqGoodsPage  $req) {
        $data = GoodsEntity::find()->where(["del_flag"=>0,"is_exchange"=>1])->orderBy("id asc");

        if ($req->getSortType()=="point" && $req->getSortValue() == 0) {
            $data->orderBy("need_points asc ");
        }
        if ($req->getSortType()=="point" && $req->getSortValue() == 1) {
            $data->orderBy("need_points desc ");
        }
        if ($req->getSortType()=="stock" && $req->getSortValue() == 0) {
            $data->orderBy("stock_num asc ");
        }
        if ($req->getSortType()=="stock" && $req->getSortValue() == 1) {
            $data->orderBy("stock_num desc ");
        }


        if ($req->getKeyword()) {
            $data->andWhere(['like', 'goods_name', $req->getKeyword()]);
        }
        $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => $req->getPageSize(),'page'=>$req->getPageNo()-1]);
        /**@var $entities GoodsEntity[]*/
        $entities = $data->offset($pages->getOffset())->limit($pages->getLimit())->all();
        $list = [];

        foreach ($entities as $entity) {
            $resp = new RespGoodsDetail();
            $resp->setGoodsName($entity->getGoodsName());
            $resp->setId($entity->getId());
            $resp->setDescTxt($entity->getDescTxt());
            $resp->setNeedPoints($entity->getNeedPoints());
            $resp->setPic($entity->getPic());
            $resp->setExchangeNum($entity->getExchangeNum());
            $resp->setStockNum($entity->getStockNum());
          //  $resp->setUserGetNum(OrderService::getUserStock($req->getUserId(),$entity->getId()));
            array_push($list,$resp);
        }

        $resp = new RespPage();
        $resp->setTotal(intval($pages->totalCount));
        $resp->setPageSize($pages->getPageSize());
        $resp->setPageNo($req->getPageNo());
        $resp->setList($list);
        return $resp;
    }





}